/**
 * Copyright (C) 2017-2018 pinganfang, Inc. All Rights Reserved.
 */
package com.pingan.haofang.searchcloud.web.test.tutorials;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.stereotype.Component;

import com.pingan.haofang.searchcloud.common.dto.PageDTO;
import com.pingan.haofang.searchcloud.common.rpc.RPCExporter;
import com.pingan.haofang.searchcloud.web.test.PageRquest;

/**
 * @author huhailiang937
 * @date 2017年6月3日下午4:40:04
 * @since 1.0.0
 */
@Component("tutorials3")
public class Tutorials03Service {

    @RPCExporter("getDataByPage")
    public PageDTO<Tutorials> getHouseByPage(PageRquest pageRquest) {
        final int total = 1050;
        final int pageSize = pageRquest.getPageSize();

        int totalPages = (total % pageSize == 0) ? total / pageSize : total / pageSize + 1;
        if (totalPages < pageRquest.getPageNo()) {
            PageDTO<Tutorials> page = PageDTO.<Tutorials>builder(Collections.emptyList()).totalPages(totalPages)
                    .totalRecords(total).pageNo(pageRquest.getPageNo()).pageSize(pageRquest.getPageSize()).build();
            return page;
        }

        int startId = pageRquest.getPageNo() * pageRquest.getPageSize();

        List<Tutorials> houses = new ArrayList<Tutorials>(pageSize);
        for (int id = startId; id < startId + pageRquest.getPageSize(); id++) {
            houses.add(HdmpHouse.build(id, Tutorials.class));
        }

        PageDTO<Tutorials> page = PageDTO.<Tutorials>builder(houses).totalPages(totalPages).totalRecords(total)
                .pageNo(pageRquest.getPageNo()).pageSize(pageRquest.getPageSize()).build();

        return page;
    }

    @RPCExporter("getDataByIds")
    public List<Tutorials> getHouseByIds(List<Integer> ids) {
        List<Tutorials> houses = new ArrayList<Tutorials>(ids.size());
        for (Integer id : ids) {
            houses.add(HdmpHouse.build(id, Tutorials.class));
        }
        return houses;
    }

    public static class Tutorials {
        private Integer id;
        private String name;
        private String location_coordinate;
        private String modifyDate;
        private int timestamp;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        /**
         * @return the location_coordinate
         */
        public String getLocation_coordinate() {
            return location_coordinate;
        }

        /**
         * @param location_coordinate the location_coordinate to set
         */
        public void setLocation_coordinate(String location_coordinate) {
            this.location_coordinate = location_coordinate;
        }

        public String getModifyDate() {
            return modifyDate;
        }

        public void setModifyDate(String modifyDate) {
            this.modifyDate = modifyDate;
        }

        public int getTimestamp() {
            return timestamp;
        }

        public void setTimestamp(int timestamp) {
            this.timestamp = timestamp;
        }
    }

}
